package top.system.dept.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import top.system.dept.pojo.Dept;
import top.system.dept.pojo.SysResp;
import top.system.dept.pojo.User;
import top.system.dept.service.UserService;
import top.system.dept.utils.RespUtil;
import top.system.dept.utils.StringUtil;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author: Amosen
 * @time: 2021-5-16
 */

@Controller
@RequestMapping("/user")
@ResponseBody
public class UserController {

	@Autowired
	private UserService userService;

	/**
	 * 处理用户登录请求
	 * @param username 用户名
	 * @param password 密码
	 * @return
	 */
	@RequestMapping(value = "/login", method = {RequestMethod.POST})
	@ResponseBody
	public SysResp handleLogin(@RequestParam("username") String username,
							   @RequestParam("password") String password,
							   @RequestParam("code") String code) {
		if (StringUtil.strCanUse(username) || StringUtil.strCanUse(password)) {
			// 进入业务处理方法
			return userService.login(username, password, code);
		} else {
			return RespUtil.fail();
		}
	}

	// TODO: 开放接口做退出登录
	@RequestMapping(value="/logout", method = {RequestMethod.POST})
	@ResponseBody
	public SysResp handleLogout(@RequestBody String userModal) {
		return userService.handleLogout(userModal);
	}

	@RequestMapping(value = "/add")
	@ResponseBody
	public SysResp handleAddUser(@RequestParam("userModal") String userModal,
								 @RequestParam("account") String account,
								 @RequestParam("auth") Integer auth,
								 @RequestParam("desc") String desc,
								 @RequestParam("deptId") Integer deptId) {
		if (!StringUtil.checkStrArgs(account, desc)) {
			return RespUtil.illegalParam();
		}
		if (auth == null || auth < 0) {
			return RespUtil.illegalParam();
		}
		if (deptId == null || deptId < 0) {
			return RespUtil.illegalParam();
		}
		// 封装对象
		User user = new User();
		user.setAccount(account);
		user.setAuth(auth);
		user.setDescription(desc);
		return userService.addUser(userModal, user, deptId);
	}

	@RequestMapping("/manager/get")
	@ResponseBody
	public SysResp handleGetManager() {
		return RespUtil.successData("users", userService.getManageUser());
	}

	@RequestMapping("/get/all")
	@ResponseBody
	public SysResp getAllMembers(@RequestParam("userModal") String userModal) {
		return userService.getMembers(userModal);
	}

	@RequestMapping("/update/mail")
	@ResponseBody
	public SysResp handleUpdateMail(@RequestParam("userModal") String userModal,
									@RequestParam("mail") String mail) {
		return userService.updateMail(userModal, mail);
	}

	@RequestMapping("/update/password")
	@ResponseBody
	public SysResp handleUpdatePassword(@RequestBody Map<String, String> data) {
		String userModal = data.get("userModal");
		String newPass = data.get("newPass");
		String oldPass = data.get("oldPass");
		return userService.updatePassword(userModal, newPass, oldPass);
	}

	@RequestMapping("/auth/get")
	@ResponseBody
	public SysResp getAuthDetail() {
		return userService.getAuthDetail();
	}

	@RequestMapping("/manager/login")
	@ResponseBody
	public SysResp managerLogin(@RequestBody User user, HttpServletResponse response) {
		SysResp sysResp = userService.managerLogin(user.getAccount(), user.getPassword());
		Cookie cookie = new Cookie("userModal", (String) sysResp.getData().get("userModal"));
		cookie.setMaxAge(12 * 60 * 60);
		response.addCookie(cookie);
		return sysResp;
	}

	@RequestMapping("/manager/getUserInfo")
	@ResponseBody
	public SysResp getUserInfo(@RequestBody Map<String, String> datas) {
		String userModal = datas.get("userModal");
		if (!userService.canUseRoot(userModal)) {
			return RespUtil.lowAuth();
		}
		User user = userService.getUserByModal(userModal);
		return RespUtil.successData("user", user);
	}

	@RequestMapping("/manager/getAllUser")
	@ResponseBody
	public SysResp getAllUser(@RequestBody Map<String, String> datas) {
		String userModal = datas.get("userModal");
		String pageNum = datas.get("pageNum");
		String pageSize = datas.get("pageSize");
		if (pageNum != null && pageSize != null) {
			try {
				int num = Integer.parseInt(pageNum);
				int size = Integer.parseInt(pageSize);
				return userService.getAllUser(num, size, userModal);
			} catch (NumberFormatException ex) {
				return RespUtil.illegalParam();
			}
		} else {
			return userService.getAllUser(null, null, userModal);
		}
	}

	@RequestMapping("/manager/deleteUser")
	@ResponseBody
	public SysResp deleteUser(@RequestBody Map<String, String> datas) {
		String userModal = datas.get("userModal");
		String userId = datas.get("userId");
		if (userId == null) {
			return null;
		} else {
			try {
				Integer id = Integer.parseInt(userId);
				return userService.deleteUser(userModal, id);
			} catch (NumberFormatException ex) {
				return RespUtil.illegalParam();
			}
		}
	}

	@RequestMapping("/manager/update")
	@ResponseBody
	public SysResp updateUser(@RequestBody Map<String, String> datas) {
		String userModal = datas.get("userModal");
		ObjectMapper om = new ObjectMapper();
		try {
			User user = om.readValue(datas.get("user"), User.class);
			return userService.updateUser(userModal, user);
		} catch (JsonProcessingException e) {
			return RespUtil.illegalParam();
		}
	}

	// TODO: 开放接口获取可转接列表
	@RequestMapping(value = "/manager/canTrans", method = {RequestMethod.GET})
	@ResponseBody
	public SysResp getCanTransMember(@RequestParam("userModal") String userModal) {
		return userService.getCanTrans(userModal);
	}

	// TODO: 开放接口进行组织转接
	@RequestMapping(value = "/manager/doTrans", method = {RequestMethod.POST})
	@ResponseBody
	public SysResp doTrans(@RequestBody Map<String, String> data) {
		String userModal = data.get("userModal");
		String userStr = data.get("user");
		ObjectMapper om = new ObjectMapper();
		try {
			User user = om.readValue(userStr, User.class);
			return userService.doTrans(userModal, user);
		} catch (JsonProcessingException e) {
			return RespUtil.illegalParam();
		}
	}
}
